package com.prs.crm.action.doc;

import java.io.File;
import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.Util;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.doc.DeptDoc;
import com.prs.crm.domain.doc.ShareDeptDoc;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.doc.DeptDocService;
import com.prs.crm.service.doc.ShareDeptDocService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class DeptDocAction extends BaseAction {

	private static final long serialVersionUID = -6753062388441613243L;
	private DeptDoc deptDoc;
	private Collection<DeptDoc> deptDocs;
	private DeptDocService deptDocService;
	private File upload;
	private String uploadFileName;
	private String typeIds;
	private PaginationSupport pagination;
	private Integer[] ids;
	private String deptIds;

	private Collection<ShareDeptDoc> shareDeptDocs;
	private ShareDeptDoc shareDeptDoc;
	private String DeptDocIds;

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;

	private Collection<CodeCommon> codelist;

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public String getDeptDocIds() {
		return DeptDocIds;
	}

	public void setDeptDocIds(String deptDocIds) {
		DeptDocIds = deptDocIds;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	private ShareDeptDocService shareDeptDocService;
	private String sharePersons;
	private String sharePersonNames;
	private Date shareBeginTime;
	private Date shareEndTime;

	@Action("editDeptDoc")
	public String editDeptDoc() {
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"单位文档");
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveDeptDoc", results = {
			@Result(name = "success", type = "redirect", location = "editDeptDoc"),
			@Result(name = "error", type = "chain", location = "editDeptDoc") })
	public String saveDeptDoc() {
		if (deptDoc.getName() == null || "".equals(deptDoc.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}	
		if (deptDoc.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (deptDoc.getOwner() == null) {
			addActionError("所有者不能为空");
			return ERROR;
		}
		if (deptDoc.getId() != null) {

		} else {
			if (deptDoc.getDeptDoccode() == null
					|| "".equals(deptDoc.getDeptDoccode().trim())) {
				addActionError("编码不能为空");
				return ERROR;
			}
			if ("n".equals(Encode)) {
				int EncodeRplice = this.getCodeService().getBackRpliceAll(
						"手动编码", "单位文档", deptDoc.getDeptDoccode());
				if (EncodeRplice != 0) {
					this.addActionError("编码重复!请重新输入");
					return ERROR;
				}
			}
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(deptDoc.getDeptDoccode());
				codes.setEntityName(deptDoc.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("单位文档");
				this.getCodeService().save(codes);
				deptDoc.setCode(codes);

			} else {
				CodeCommon Vob = this.getCodeCommonService().getVobject(
						codeCommon.getId());
				Code codes = new Code();
				codes.setEntityName(deptDoc.getClass().getName());
				codes.setCodeValue(deptDoc.getDeptDoccode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.codeCommonService.saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("单位文档");
				this.getCodeService().save(codes);
				deptDoc.setCode(codes);

			}
		}
		this.getDeptDocService().saveDeptDoc(deptDoc, upload, uploadFileName);
		this.setPrompt("提交文档成功");

		return SUCCESS;
	}

	@Action("listDeptDoc")
	public String listDeptDoc() {
		return SUCCESS;
	}

	@Action("listDeptDocByType")
	public String listDeptDocByType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DeptDoc.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.in("type", this.getDeptDocService()
				.convertToDeptDocType(typeIds)));
		this.setPagination(this.getDeptDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listShareDeptDoc")
	public String listShareDeptDoc() {
		return SUCCESS;
	}

	@Action("listShareDeptDocByType")
	public String listShareUserDocByType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DeptDoc.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.in("type", this.getDeptDocService()
				.convertToDeptDocType(typeIds)));
		this.setPagination(this.getDeptDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewDeptDoc")
	public String viewDeptDoc() {
		return SUCCESS;
	}

	@Action("editShareDeptDoc")
	public String editShareDeptDoc() {
		this.codelist = this.getCodeCommonService()
				.getCodeCommonByCodeType("C");
		/*
		 * shareDeptDocs =
		 * this.getShareDeptDocService().getShareDeptDoc(deptDoc);
		 * this.sharePersons = null; this.sharePersonNames = null;
		 * for(ShareDeptDoc doc:shareDeptDocs) { this.shareBeginTime =
		 * doc.getBeginTime(); this.shareEndTime = doc.getEndTime();
		 * 
		 * sharePersonNames = (sharePersonNames ==
		 * null)?doc.getPerson().getResume().getName():sharePersonNames + "," +
		 * doc.getPerson().getResume().getName(); sharePersons = (sharePersons
		 * == null)?doc.getPerson().getId().toString():sharePersons + "," +
		 * doc.getPerson().getId().toString(); }
		 */

		deptDocs = this.getDeptDocService().convertToDeptDoc(ids);
		deptIds = "";
		for (DeptDoc dd : deptDocs) {
			deptIds += dd.getId() + ",";
		}
		return SUCCESS;
	}

	@Action(value = "submitShareDeptDoc", results = {
			@Result(name = "success", type = "chain", location = "listShareDeptDoc"),
			@Result(name = "error", type = "chain", location = "editShareDeptDoc") })
	public String submitShareDeptDoc() {
		if (shareDeptDoc.getBeginTime() == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (shareDeptDoc.getEndTime() == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (sharePersons == null || "".equals(sharePersons.trim())) {
			addActionError("发放人员不能为空");
			return ERROR;
		}
		typeIds = "-1,";
		this.getShareDeptDocService().saveShareDeptDoc(shareDeptDoc, deptIds,
				sharePersons);
		return SUCCESS;
	}

	@Action("listMyShareDeptDoc")
	public String listMyShareDeptDoc() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ShareDeptDoc.class);
		// criteria.add(Restrictions.eq("person",
		// AppContext.getAccountContext().currentUser()));
		criteria.add(Restrictions.le("beginTime", Util.nowFullTime()));// beginTime小于等于当前时间
		criteria.add(Restrictions.ge("endTime", Util.nowFullTime()));// endTime大于等于当前时间
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getShareDeptDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "removeDeptDoc", results = { @Result(name = "success", type = "chain", location = "listDeptDocByType") })
	public String removeDeptDoc() {
		typeIds = "-1,";
		this.getDeptDocService().batchRemove(ids);
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "removeShareDeptDoc", results = { @Result(name = "success", type = "redirect", location = "listMyShareDeptDoc") })
	public String removeShareDeptDoc() {
		this.setPrompt("删除共享记录成功");
		this.getShareDeptDocService().batchRemove(ids);
		return SUCCESS;
	}

	@Action("listChangeDeptDoc")
	public String listChangeDeptDoc() {
		return SUCCESS;
	}

	@Action("listChangeDeptDocByType")
	public String listChangeDeptDocByType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DeptDoc.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.in("type", this.getDeptDocService()
				.convertToDeptDocType(typeIds)));
		this.setPagination(this.getDeptDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	public DeptDoc getDeptDoc() {
		return deptDoc;
	}

	public void setDeptDoc(DeptDoc deptDoc) {
		this.deptDoc = deptDoc;
	}

	public DeptDocService getDeptDocService() {
		return deptDocService;
	}

	public void setDeptDocService(DeptDocService deptDocService) {
		this.deptDocService = deptDocService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getTypeIds() {
		return typeIds;
	}

	public void setTypeIds(String typeIds) {
		this.typeIds = typeIds;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Collection<ShareDeptDoc> getShareDeptDocs() {
		return shareDeptDocs;
	}

	public void setShareDeptDocs(Collection<ShareDeptDoc> shareDeptDocs) {
		this.shareDeptDocs = shareDeptDocs;
	}

	public ShareDeptDoc getShareDeptDoc() {
		return shareDeptDoc;
	}

	public void setShareDeptDoc(ShareDeptDoc shareDeptDoc) {
		this.shareDeptDoc = shareDeptDoc;
	}

	public ShareDeptDocService getShareDeptDocService() {
		return shareDeptDocService;
	}

	public void setShareDeptDocService(ShareDeptDocService shareDeptDocService) {
		this.shareDeptDocService = shareDeptDocService;
	}

	public String getSharePersons() {
		return sharePersons;
	}

	public void setSharePersons(String sharePersons) {
		this.sharePersons = sharePersons;
	}

	public String getSharePersonNames() {
		return sharePersonNames;
	}

	public void setSharePersonNames(String sharePersonNames) {
		this.sharePersonNames = sharePersonNames;
	}

	public Date getShareBeginTime() {
		return shareBeginTime;
	}

	public void setShareBeginTime(Date shareBeginTime) {
		this.shareBeginTime = shareBeginTime;
	}

	public Date getShareEndTime() {
		return shareEndTime;
	}

	public void setShareEndTime(Date shareEndTime) {
		this.shareEndTime = shareEndTime;
	}

	public Collection<DeptDoc> getDeptDocs() {
		return deptDocs;
	}

	public void setDeptDocs(Collection<DeptDoc> deptDocs) {
		this.deptDocs = deptDocs;
	}

	public String getDeptIds() {
		return deptIds;
	}

	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
